package com.kokomi.controller;



import com.kokomi.pojo.VO.CartVo;
import com.kokomi.service.CartService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cart")
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;


    /**
     * 查询购物车
     */
    @GetMapping("/{userId}")
    @ApiOperation("查询购物车")
    public ResponseEntity<CartVo> getCart(@PathVariable("userId") long userId) {

        log.info("用户ID:{}", userId);
        //调用业务层查询购物车
        CartVo cart = cartService.getCart(userId);
        log.info("购物车信息:{}", cart);
        return ResponseEntity.ok(cart);
    }

    /**
     * 添加商品到购物车
     */
    @GetMapping("/add/{userId}/{bookId}")
    @ApiOperation("添加商品到购物车")
    public ResponseEntity<CartVo> addCartItem(
            @PathVariable("userId") Long userId,
            @PathVariable("bookId") Long bookId) {

        //调用业务层添加商品到购物车
        CartVo cart = cartService.addCartItem(userId, bookId);
        return ResponseEntity.ok(cart);
    }

    /**
     * 删除购物车中的商品
     */
    @GetMapping("/remove/{userId}/{bookId}")
    @ApiOperation("删除购物车中的商品")
    public ResponseEntity<CartVo> removeCartItem(
            @PathVariable("userId") Long userId,
            @PathVariable("bookId") Long bookId) {
        //调用业务层删除购物车中的商品
        CartVo cart = cartService.removeCartItem(userId, bookId);
        return ResponseEntity.ok(cart);
    }
    /**
     * 更新购物车中商品的数量
     */
    @GetMapping("/update/{userId}/{bookId}/{quantity}")
    @ApiOperation("更新购物车中商品的数量")
    public ResponseEntity<CartVo> updateCartItemQuantity(
            @PathVariable("userId") Long userId,
            @PathVariable("bookId") Long bookId,
            @PathVariable("quantity") Integer quantity) {
        //调用业务层更新购物车中商品的数量
        CartVo cart = cartService.updateCartItemQuantity(userId, bookId, quantity);
        return ResponseEntity.ok(cart);
    }

    /**
     * 清空购物车
     */
    @GetMapping("/clear/{userId}")
    @ApiOperation("清空购物车")
    public ResponseEntity<String> clearCart(@PathVariable("userId") Long userId) {
        //调用业务层清空购物车
        cartService.clearCart(userId);
        return ResponseEntity.ok("购物车以清空");

    }

}
